using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using Nebulosa.Models;

namespace Web.Controllers {
    public class InstallController : Controller {
		public ActionResult Index() {
			return RedirectToAction("Language");
		}


		[HttpGet]
		public ActionResult Language(string language) {
			CultureInfo cultureInfo = null;

			if (!string.IsNullOrEmpty(language)) {
				cultureInfo = new CultureInfo(language);
			} else if (string.IsNullOrEmpty(language)) {
				cultureInfo = new CultureInfo(Nebulosa.Core.Constants.DefaultCulture);
			}

			Session[Nebulosa.Core.Constants.SessionCulture] = cultureInfo;
			Thread.CurrentThread.CurrentUICulture = cultureInfo;
			Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);

			ViewBag.cultureInfo = cultureInfo;

			return View("Nebulosa/Language");
		}

		[HttpPost]
		public ActionResult Language() {
			return RedirectToAction("Database");
		}


		[HttpGet]
		public ActionResult Database() {
			return View("Nebulosa/Database");
		}

		[HttpPost]
		public ActionResult Database(string database) {
			return RedirectToAction("DatabaseConfig", new { id = database });
		}

		[HttpGet]
		public ActionResult DatabaseConfig(string id) {
			DatabaseConfigModel model = new DatabaseConfigModel();
			
			model.DatabaseType = id;
		
			ViewBag.database = id;	
			return View("Nebulosa/DatabaseConfig", model);
		}

		[HttpPost]
		public ActionResult DatabaseConfig(DatabaseConfigModel model) {
			string connectionString = "";
			string provider = "";

			if(model.DatabaseType == "sqlite") {
				throw new NotImplementedException("sqlite data abstraction not implemented yet");
			} else if(model.DatabaseType == "sqlserver") {
				throw new NotImplementedException("sqlserver data abstraction not implemented yet");
			} else if(model.DatabaseType == "azuresql") {
				throw new NotImplementedException("azuresql data abstraction not implemented yet");
			} else if(model.DatabaseType == "mysql") {
				throw new NotImplementedException("mysql data abstraction not implemented yet");
			} else if(model.DatabaseType == "nebulosadb") {
				//model.Insert();
				
				DatabaseConfigModel dcm = new DatabaseConfigModel();
				dcm.DatabaseType = "nebulosadb";
				dcm.Insert();
				return Json(dcm.Get());
				
				//Nebulosa.Data.Providers.DbProvider.Trace = "";
				
				//return Json(new { obj = dcm.Get(), trace = Nebulosa.Data.Providers.DbProvider.Trace });
					
				//return Content(Nebulosa.Data.Providers.DbProvider.Trace);
				
				throw new NotImplementedException("nebulosadb data abstraction not implemented yet");
			}
			
			return Content("-" + model.DatabaseType);
			//return View("Nebulosa/DatabaseConfig");
		}
    }
}